package com.mspbots.ticketai;

import com.mspbots.api.page.Page;
import com.mspbots.ticketai.model.Ticket;
import com.mspbots.ticketai.model.TicketOnly;
import com.mspbots.ticketai.model.TicketRule;
import com.mspbots.ticketai.model.TicketSchedule;
import com.mspbots.ticketai.service.TicketService;
import lombok.RequiredArgsConstructor;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * com.mspbots.core.ticketai.TicketController
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2019/8/8
 */
@RestController
@RequestMapping("tickets")
@RequiredArgsConstructor
public class TicketController {

    private final TicketService ticketService;

    @GetMapping("page-db")
    public Mono<Page<TicketOnly>> pageNew(@RequestParam Map<String, Object> params, Page<TicketOnly> pageable) {
        Assert.notNull(params.get("tenantId"), "Request params [tenantId] not null.");
        Assert.notNull(params.get("board"), "Request params [board] not null.");
        if (StringUtils.isEmpty(params.get("identifier")) || StringUtils.isEmpty(params.get("allAssigned"))) {
            params.put("allAssigned", false);
        }
        return this.ticketService.page(params, pageable);
    }

    @GetMapping("assigned-ticket-page")
    public Mono<Page<TicketSchedule>> assignedTicketlist(@RequestParam Map<String, Object> params, Page<TicketSchedule> pageable) {
        Assert.notNull(params.get("tenantId"), "Request params [tenantId] not null.");
        Assert.notNull(params.get("identifier"), "Request params [identifier] not null.");
        return this.ticketService.assignedTicketPage(params, pageable);
    }

    @GetMapping("next-ticket-db")
    public Mono<TicketOnly> requestNextTicket(@RequestParam Map<String, Object> params) {
        Assert.notNull(params.get("tenantId"), "Request Params [tenantId] not null");
        Assert.notNull(params.get("board"), "Request params [board] not null.");
        Assert.notNull(params.get("username"), "Request params [username] not null.");
        if (StringUtils.isEmpty(params.get("identifier")) || StringUtils.isEmpty(params.get("allAssigned"))) {
            params.put("allAssigned", false);
        }
        return this.ticketService.nextTicket(params);
    }

    @PostMapping("/nextTicketClicked")
    public void nextTicketClicked(@RequestBody Map<String, Object> params) {
        Assert.notNull(params.get("tenantId"), "Request Params [tenantId] not null");
        Assert.notNull(params.get("userId"), "Request Params [userId] not null");
        Assert.notNull(params.get("cwId"), "Request Params [cwId] not null");
        Assert.notNull(params.get("cwId"), "Request Params [cwId] not null");
    }

    @PostMapping("page-single-rule")
    public Mono<Page<TicketOnly>> pageBySingleRule(@RequestParam Map<String, Object> params, @RequestBody TicketRule ticketRule, Page<TicketOnly> pageable) {
        Assert.notNull(params.get("tenantId"), "Request params [tenantId] not null.");
        Assert.notNull(params.get("board"), "Request params [board] not null.");
        if (StringUtils.isEmpty(params.get("identifier")) || StringUtils.isEmpty(params.get("allAssigned"))) {
            params.put("allAssigned", false);
        }
        return this.ticketService.pageBySingleRule(params, pageable, ticketRule);
    }

    @GetMapping("check-status")
    public Flux<Map<String,Object>> getCheckStatus(Ticket ticket) {
        Assert.notNull(ticket.getId(), () -> "Search param [Id] not null.");
        Assert.notNull(ticket.getCwId(), () -> "Search param [cwId] not null.");
        Assert.notNull(ticket.getTenantId(), () -> "Search param [tenantId] not null.");
        Assert.notNull(ticket.getStatusId(), () -> "Search param [statusId] not null.");
        return this.ticketService.getCheckStatus(ticket);
    }
}
